<?php

namespace Psg;

import('Psg.Auth');

class Core {

    /**
     * Zwraca połączenie z bazą danych
     * 
     * @return \Db\Connection Obiekt połączenia
     */
    private static function dbHandler() {
        $Db = \Db\Connection::Open();
        return $Db;
    }

    public static function timestampToWord($timestamp) {
        $time = strtotime($timestamp);
        $now = time();
        $delay = $now - $time;
        $minutes = (int) ($delay / 60);
        $hrs = (int) ($delay / 3600);
        $days = (int) ($delay / 86400);
        $weeks = $days * 7;
        $mths = $weeks * 4;
        if ($delay <= 60) {
            $return = 'minutę temu';
        } else
        if ($minutes > 1 && $minutes < 5) {
            $return = $minutes . ' minuty temu';
        } else if ($minutes >= 5 && $minutes < 60) {
            $return = $minutes . ' minut temu';
        } else if ($minutes >= 60 && $minutes < 120) {
            $return = 'godzinę temu';
        } else if ($hrs >= 2 && $hrs < 5) {
            $return = $hrs . ' godziny temu';
        } else if ($hrs >= 5 && $hrs < 24) {
            $return = $hrs . ' godzin temu';
        } else if ($days == 1) {
            $return = 'dzień temu';
        } else if ($days >= 2 && $days < 31) {
            $return = $days . ' dni temu';
        } else if ($mths == 1) {
            $return = 'miesiąc temu';
        } else if ($mths >= 2 && $mths < 5) {
            $return = $mths . ' miesiące temu';
        } else if ($mths == 5) {
            $returm = $mths . 'miesięcy temu';
        } else {
            $return = 'ponad pół roku temu';
        }

        return $return;
    }

    public static function lastActionUpdate($email) {
        $Query = \Db\Query::Create('uzytk')->Update()
                ->Set(\Db\Operators::Create()
                        ->C('last_act')->O('=')->V(time()))
                ->Where(\Db\Operators::Create()
                ->C('email')->O('=')->V($email));
        self::dbHandler()->Query($Query);
    }

    public static function getLastAction($email) {
        $Query = \Db\Query::Create('uzytk')->Select(['last_act'])
                ->Where(\Db\Operators::Create()
                ->C('email')->O('=')->V($email));
        return self::dbHandler()->Query($Query)[0]['last_act'];
    }

    public static function isUserEnabled($email) {
        if (Auth::userExists($email)) {
            $Query = \Db\Query::Create('uzytk')->Select(['enable'])
                    ->Where(\Db\Operators::Create()
                    ->C('email')->O('=')->V($email));
            $ret = self::dbHandler()->Query($Query)[0]['enable'];
            if ($ret == 1)
                return true;
            else
                return false;
        } else {
            return false;
        }
    }

    public static function getUserInfo($email) {
        if (Auth::userExists($email)) {
            $Query = \Db\Query::Create('uzytk')
                    ->Select()
                    ->Where(\Db\Operators::Create()
                    ->C('email')->O('=')->V($email));
            $Return = self::dbHandler()->Query($Query);
            if (count($Return) == 1) {
                return $Return[0];
            } else {
                return 'PSG:USRDATADAMAGED';
            }
        } else {
            return 'PSG:USRNOTEXISTS';
        }
    }

    public static function getUserIdByMail($email) {
        if (is_array($In = self::getUserInfo($email)))
            return $In['id'];
        else
            return 'PSG:USRNOTEXISTS';
    }

    public static function getUserMailById($id) {
        $Query = \Db\Query::Create('uzytk')
                ->Select(['email'])
                ->Where(\Db\Operators::Create()
                ->C('id')->O('=')->V($id));
        $Return = self::dbHandler()->Query($Query);
        if (count($Return) == 1) {
            return $Return[0]['email'];
        } else {
            return 'PSG:USRNOTEXISTS';
        }
    }

    public static function getUserRole($email) {
        if (Auth::userExists($email)) {
            return self::getUserInfo($email)["role"];
        } else {
            return 'PSG:USRNOTEXISTS';
        }
    }

    public static function getRegistrationInfo($email) {
        if (Auth::registrationExist($email)) {
            $Query = \Db\Query::Create('registrations')
                    ->Select()
                    ->Where(\Db\Operators::Create()
                    ->C('email')->O('=')->V($email));
            $Return = self::dbHandler()->Query($Query);
            if (count($Return) == 1) {
                return $Return[0];
            } else {
                return 'PSG:REGDATADAMAGED';
            }
        } else {
            return 'PSG:REGNOTEXISTS';
        }
    }

    public static function delRegistrationData($email) {
        $Query = \Db\Query::Create('registrations')
                ->Delete()
                ->Where(\Db\Operators::Create()
                ->C('email')->O('=')->V($email));
        self::dbHandler()->Query($Query);
    }

    public static function registerUser($email) {
        $Arr = self::getRegistrationInfo($email);
        if (is_array($Arr)) {
            $cols = [
                'email', 'nick', 'id_woj', 'id_miasto', 'role', 'enable'
            ];
            $values = [
                $Arr['email'],
                $Arr['nick'],
                $Arr['state'],
                $Arr['city'],
                'user',
                1
            ];
            $oper = \Db\Operators::Create()
                    ->O('?')->O(',')
                    ->O('?')->O(',')
                    ->O('?')->O(',')
                    ->O('?')->O(',')
                    ->O('?')->O(',')
                    ->O('?');
            $Query = \Db\Query::Create('uzytk')
                    ->Insert($oper, $cols);
            self::dbHandler()->Prepare($Query, $values);
            self::delRegistrationData($email);
            return true;
        } else {
            return false;
        }
    }

}
